//
//  TitleView.m
//  Project_A Gary
//
//  Created by mac on 16/3/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "TitleView.h"
@interface TitleView()

@property(nonatomic, retain)NSArray *buttonArray;
@property(nonatomic,retain)UIView *indicatorView;/**< 选中框 */
@property(nonatomic, retain)UIButton *selectButton;
@property(nonatomic, assign)BOOL isClick;

@end
@implementation TitleView
+(id)creatTitleView
{
    return [[self alloc]init];
}
-(instancetype)init
{
    if (self = [super init]) {
        //[self createIndicatorView];
    }
    return self;
}
-(UIButton *)selectedButton
{
    return _buttonArray[_selected];
}
-(void)setTitleArray:(NSArray *)titleArray
{
    NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:titleArray.count];
    _titleArray = titleArray;
    float x = 0;
    for (NSString *title in titleArray) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        button.backgroundColor = [UIColor whiteColor];
        // 设置每个button的大小
        button.frame = CGRectMake(x, 0, 65, 50);
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        button.layer.cornerRadius = 4;
        button.clipsToBounds = YES;
        [self addSubview:button];
        [buttonArray addObject:button];
        if (x == 0) {
            button.selected = YES;
        }
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        x += 65;
    }
    self.buttonArray = buttonArray;
    self.contentSize = CGSizeMake(x, 1);
    
    
    
    
}
-(void)buttonClicked:(UIButton *)button
{
    NSUInteger newIndex = [_buttonArray indexOfObject:button];
    if (newIndex == _selected) {
        return;
    }
    _selected = newIndex;
    [self setSelectedIndex:newIndex];
}
-(void)setSelectedIndex:(NSUInteger)selectedIndex
{
    self.selected = selectedIndex;
    for (UIButton * button in self.buttonArray) {
        button.selected = NO;
    }
    self.selectedButton.selected = YES;
    [UIView animateWithDuration:0.2 animations:^{
        // 滚动到下一类别
        CGRect  rect = [self  convertRect: CGRectMake(self.selectedButton.frame.origin.x-self.frame.size.width/2, self.selectedButton.frame.origin.y, self.frame.size.width, self.selectedButton.frame.size.height) toView:self];
        [self scrollRectToVisible:rect  animated:YES];
    }];
     //-(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
    if (self.delegates && [self.delegates respondsToSelector:@selector(titleView:selectedIndexChanged:)]) {
        
        //代理3.代理人实现方法
        [self.delegates titleView:self selectedIndexChanged:selectedIndex];
         }

}
//通知接收者他的父视图将会改变到特定的父视图
//参数 newSuperview 新的视图对象将会是接收者新的父视图
// titleView的大小
- (void)willMoveToSuperview:(UIView *)newSuperview{
    
    self.frame = CGRectMake(0, 64, newSuperview.frame.size.width, 44);
    
}




@end
